package com.shanghaionstar.user;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;

import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.mag.PlateEngineNumEntity;
import com.onstar.cn.weight.DeleteEditText;
import com.onstar.cn.weight.ProgressLayout;
import com.onstar.cn.weight.ToastView;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.MoreActivity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.user.iview.ISetPlateNumberView;
import com.shanghaionstar.user.presenter.SetPlateNumberPresenter;
import com.shanghaionstar.utils.A2bigA;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.weight.CommonProgressButton;
import com.shanghaionstar.weight.PlateNumberProvinceLayout;
import com.shanghaionstar.weight.TopLayout;

import java.util.Locale;


/**
 * Created by 7828wc on 2016/6/30.
 * 设置车牌号发动机号界面，通过PlateAndEngineEnum区分是车牌号还是发动机号修改
 */
public class SetPlateNumberFragment extends BaseFragment implements ISetPlateNumberView {
    @FindViewById
    private ProgressLayout progress_layout;
    @FindViewById
    private TopLayout top_layout;
    @FindViewById
    private Button btn_plate_province;
    @FindViewById
    private DeleteEditText et_plate_number;
    @FindViewById
    private TextView tv_wrong_plate_number, tag_tv;
    @FindViewById
    private PlateNumberProvinceLayout layout_bottom_pop;
    @FindViewById
    private LinearLayout plate_num_ll;
    @FindViewById
    private CommonProgressButton btn_submit;

    private SetPlateNumberPresenter presenter;
    private MoreActivity parentActivity;
    private String plateEngineFlag = "";
    private String editTextValue = "";

    @Override
    public int getLayoutId() {
        return R.layout.more_plate_number;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        parentActivity = (MoreActivity) activity;
    }

    private boolean isAnimating;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (isFirstLoad()) {
            presenter = new SetPlateNumberPresenter(parentActivity.getScheduler());
            presenter.setView(this);

            top_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (PlateAndEngineEnum.PlateNumber.name().equals(plateEngineFlag))
                        BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.My_updatetosubscriber.getStringValue());
                    CommonUtil.hideKeyBoard(getActivity(), et_plate_number);
                    back();
                }
            });

            layout_bottom_pop.setPopItemClickListener(new PlateNumberProvinceLayout.PopItemClickListener() {
                @Override
                public void onPopItemClickListener(String plateType) {
//                    if(!isAnimating) {
//                        isAnimating = true;
                    presenter.popAnimationWithKeyboard(false);
                    btn_plate_province.setText(plateType);
//                        int height = layout_bottom_pop.getMeasuredHeight();
//                        AnimatorSet animatorSet = new AnimatorSet();
//                        animatorSet.setInterpolator(new DecelerateInterpolator());
//
//                        DisplayMetrics metrics = new DisplayMetrics();
//                        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
//
//                        ObjectAnimator translationY = ObjectAnimator.ofFloat(plate_num_ll, "translationY", metrics.heightPixels - plate_num_ll.getHeight() - height / 2, plate_num_ll.getTranslationY());
//                        ObjectAnimator translationX = ObjectAnimator.ofFloat(plate_num_ll, "translationX", (metrics.widthPixels - plate_num_ll.getHeight()) / 2, plate_num_ll.getTranslationX());
//                        animatorSet.play(translationY).with(translationX).with(ObjectAnimator.ofFloat(plate_num_ll, "scaleX", 5.0f, 2f)).with(ObjectAnimator.ofFloat(plate_num_ll, "scaleY", 5.0f, 2f));
//                        animatorSet.play(ObjectAnimator.ofFloat(plate_num_ll, "scaleX", 2.0f, 1f)).with(ObjectAnimator.ofFloat(plate_num_ll, "scaleY", 2.0f, 1f));
//                        animatorSet.play(ObjectAnimator.ofFloat(plate_num_ll, "scaleX", 2.0f, 1f)).after(translationY);
//                        animatorSet.setDuration(500);
//                        animatorSet.addListener(new Animator.AnimatorListener() {
//                            @Override
//                            public void onAnimationStart(Animator animator) {
//                                btn_submit.setVisibility(View.GONE);
//                            }
//
//                            @Override
//                            public void onAnimationEnd(Animator animator) {
//                                btn_submit.setVisibility(View.VISIBLE);
//                                isAnimating = false;
//                            }
//
//                            @Override
//                            public void onAnimationCancel(Animator animator) {
//
//                            }
//
//                            @Override
//                            public void onAnimationRepeat(Animator animator) {
//
//                            }
//                        });
//                        animatorSet.start();
//                    }
                }
            });

            layout_bottom_pop.setOnFinishButtonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    layout_bottom_pop.setVisibility(View.GONE);
                }
            });

            if (getArguments() != null) {
                plateEngineFlag = getArguments().getString(FinalConstants.PlateEngineNumber.PLATE_ENGINE_FLAG);
                String plageEngineNumber = getArguments().getString(plateEngineFlag);
                //判断如果当前是需要切换到车牌号修改界面
                if (!CommonUtil.isNull(plateEngineFlag) && plateEngineFlag.equalsIgnoreCase(PlateAndEngineEnum.PlateNumber.name())) {
                    plate_num_ll.setVisibility(View.VISIBLE);
                    tag_tv.setText(R.string.option_plate_number);
                    if (!CommonUtil.isNull(plageEngineNumber)) {
                        btn_plate_province.setText(plageEngineNumber.substring(0, 1));
                        et_plate_number.setText(plageEngineNumber.substring(1));
                    }
                    et_plate_number.setHint(R.string.option_set_plate_number_hint);
                    top_layout.setTopTitle(getString(R.string.option_set_plate_number));
                } else if (!CommonUtil.isNull(plateEngineFlag) && plateEngineFlag.equalsIgnoreCase(PlateAndEngineEnum.EngineNumber.name())) {
                    //当前是修改发动机号界面
                    plate_num_ll.setVisibility(View.GONE);
                    layout_bottom_pop.setVisibility(View.GONE);
                    tag_tv.setText(R.string.option_engine);
                    top_layout.setTopTitle(getString(R.string.option_set_engine_number));
                    et_plate_number.setHint(R.string.option_set_engine_number_hint);
                    et_plate_number.setText(plageEngineNumber);
                } else {
                    //传入其他值忽略
                }
            }
        }
        setListener();
    }

    private void setListener() {
        et_plate_number.setTransformationMethod(new A2bigA());
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String plateEngineNumber = "";
                if (!CommonUtil.isNull(plateEngineFlag) && plateEngineFlag.equalsIgnoreCase(PlateAndEngineEnum.EngineNumber.name())) {
                    plateEngineNumber = et_plate_number.getText().toString();
                    if (CommonUtil.isNull(plateEngineNumber)) {
                        tv_wrong_plate_number.setText(R.string.option_wrong_engine_number_hint);
                        tv_wrong_plate_number.setVisibility(View.VISIBLE);
                        return;
                    }
                    if (!CommonUtil.checkInput(plateEngineNumber)) {
                        tv_wrong_plate_number.setText(R.string.option_wrong_engine_number_hint);
                        tv_wrong_plate_number.setVisibility(View.VISIBLE);
                        return;
                    }
                    if (plateEngineNumber.length() < 6 || plateEngineNumber.length() > 20) {
                        tv_wrong_plate_number.setText(R.string.option_wrong_engine_number_hint);
                        tv_wrong_plate_number.setVisibility(View.VISIBLE);
                        return;
                    }
                } else {
//                    plateEngineNumber = new StringBuilder().append(btn_plate_province.getText().toString()).append(et_plate_number.getText().toString()).toString().toUpperCase(Locale.CHINA);
                    plateEngineNumber = (btn_plate_province.getText().toString() + et_plate_number.getText().toString()).toUpperCase(Locale.CHINA);
                    if (CommonUtil.isNull(plateEngineNumber)) {
                        tv_wrong_plate_number.setText(R.string.option_wrong_plate_number_hint);
                        tv_wrong_plate_number.setVisibility(View.VISIBLE);
                        return;
                    }
                    if (!CommonUtil.checkInput(et_plate_number.getText().toString())) {
                        tv_wrong_plate_number.setText(R.string.option_wrong_plate_number_hint);
                        tv_wrong_plate_number.setVisibility(View.VISIBLE);
                        return;
                    }
                    if (!CommonUtil.isPlateNumber(plateEngineNumber)) {
                        tv_wrong_plate_number.setText(R.string.option_wrong_plate_number_hint);
                        tv_wrong_plate_number.setVisibility(View.VISIBLE);
                        return;
                    }
                }
                CommonUtil.hideKeyBoard(onActivity(), et_plate_number);
                tv_wrong_plate_number.setVisibility(View.INVISIBLE);
                editTextValue = plateEngineNumber;
                BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Plate_Save.getStringValue());
                presenter.updateVehicleInfo(editTextValue, plateEngineFlag);
                presenter.start();
            }
        });
    }

    @OnClick({R.id.btn_plate_province})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_plate_province:
                if (layout_bottom_pop.getVisibility() == View.VISIBLE) {
                    layout_bottom_pop.setVisibility(View.GONE);
                } else {
                    layout_bottom_pop.setVisibility(View.VISIBLE);
                }
                presenter.popAnimationWithKeyboard(layout_bottom_pop.getVisibility() == View.VISIBLE);
                CommonUtil.hideKeyBoard(getActivity(), et_plate_number);
                break;
            default:
                break;
        }
    }


    @Override
    public void popAnimationWithKyeboard(Animation animation, boolean isOpen) {
        layout_bottom_pop.clearAnimation();
        int height = layout_bottom_pop.getMeasuredHeight();
        ViewGroup.MarginLayoutParams bottomMargins = (ViewGroup.MarginLayoutParams) layout_bottom_pop.getLayoutParams();
        bottomMargins.bottomMargin = isOpen ? 0 : -height;
        layout_bottom_pop.setLayoutParams(bottomMargins);
        layout_bottom_pop.setAnimation(animation);
        layout_bottom_pop.startAnimation(animation);
    }

    @Override
    public PlateNumberProvinceLayout getProvinceLayout() {
        return layout_bottom_pop;
    }

    @Override
    public void setLoading(boolean isLoading) {
        btn_submit.showLoading(isLoading);
    }

    @Override
    public void onResponseError(OnStarException e) {
        tv_wrong_plate_number.setText(R.string.option_submit_failure);
        tv_wrong_plate_number.setVisibility(View.VISIBLE);
    }

    @Override
    public void onReponseSuccess(PlateEngineNumEntity object) {
        if (!CommonUtil.isNull(plateEngineFlag)) {
            int resId = R.string.option_change_plate_success;
            if (!CommonUtil.isNull(plateEngineFlag) && plateEngineFlag.equalsIgnoreCase(PlateAndEngineEnum.PlateNumber.name())) {
                parentActivity.setPlateNumber(editTextValue);
                resId = R.string.option_change_plate_success;
            } else if (!CommonUtil.isNull(plateEngineFlag) && plateEngineFlag.equalsIgnoreCase(PlateAndEngineEnum.EngineNumber.name())) {
                parentActivity.setEngineNumber(editTextValue);
                resId = R.string.option_change_engine_success;
            }
            ToastView toastView = new ToastView(getActivity());
            toastView.showToast(getString(resId));
            PreferencesUtls.getInstance(parentActivity).putString("engineNumber", parentActivity.getEngineNumber());
            PreferencesUtls.getInstance(parentActivity).putString("plateNumber", parentActivity.getPlateNumber());
            back();
        }

    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }
}